//
//  AcceptedOrderVC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/23.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import SwiftyJSON
import Moya

class AcceptedOrderVC: BaseVC {

  var parentVC: UIViewController!
  
  fileprivate var workOrders: [WorkOrder] = []
  
  fileprivate var tableView: UITableView?
  fileprivate var cancle: Cancellable?
  
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = GlobalConfig.BGWhiteColor
  
    create()
  }
  
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
  deinit {
    cancle?.cancel()
  }
  
}


// MARK: - action

extension AcceptedOrderVC {
  
    func cleanData() {
        workOrders = []
        tableView?.reloadData()
    }
    
    func updateData(by isHeader: Bool) {
    cancle = WorkorderRequest.shared.getCurrentOrder(success: { (orders) in
      self.workOrders = orders
      runOnMainThread {
        if isHeader {
            self.tableView?.mj_header.endRefreshing()
        }
        self.tableView?.reloadData()
      }
    }) {
        if isHeader {
            runOnMainThread {
                self.tableView?.mj_header.endRefreshing()
            }
        }
    }
  }
}

// MARK: - fileprivate

extension AcceptedOrderVC {
  
  fileprivate func create() {
    
    tableView = UITableView()
    tableView?.backgroundColor = GlobalConfig.BGWhiteColor
    tableView?.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.noBarHeight - 44)
    tableView?.delegate = self
    tableView?.dataSource = self
    tableView?.separatorStyle = .none
    tableView?.register(WorkOrderCell.self, forCellReuseIdentifier: WorkOrderCell.identifier)
    
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 10))
    footerView.backgroundColor = GlobalConfig.BGWhiteColor
    tableView?.tableFooterView = footerView
    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 4))
    headerView.backgroundColor = GlobalConfig.BGWhiteColor
    tableView?.tableHeaderView = headerView
    
    view.addSubview(tableView!)
    
    tableView?.mj_header = CustomRefreshHeader(refreshingBlock: { [weak self] in self?.updateData(by: true) })
  }
}

// MARK: - UITableViewDataSource

extension AcceptedOrderVC : UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return workOrders.count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: WorkOrderCell.identifier) as! WorkOrderCell
    cell.model = workOrders[indexPath.row]
    return cell
  }
}

// MARK: - UITableViewDelegate

extension AcceptedOrderVC : UITableViewDelegate {
  
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return WorkOrderCell.height(with: workOrders[indexPath.row])
  }
  
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    let vc = OrderDetailBaseVC()
    vc.orderId = workOrders[indexPath.row].id
    vc.parentVC = parentVC
    vc.orderStatus = OrderHelper.getType(order: workOrders[indexPath.row])
    vc.refreshBeforeClosure = {
        self.updateData(by: false)
    }
    parentVC.navigationController?.pushViewController(vc, animated: true)
    
  }
  
  func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    let fixView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 1))
    fixView.backgroundColor = GlobalConfig.BGWhiteColor
    return fixView
    
  }
  
  func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 1
  }
  
}




